// pages/voteDetail/voteDetailReq.js
import {electionVote, GetVoteDetail} from '../../services/voteDetailReq'
import {getDetail} from "../../services/contentDetailReq";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        content: {},
        result: null,
        options: null,
        article: "",
    },


    onLoad: function (options) {
        wx.showLoading({
            title: "加载中"
        })
        let {article} = this.data;
        GetVoteDetail(options.id, res => {
            wx.hideLoading()
            const {data} = res;
            const {articlelink} = data.ElectionEntity;

            if (articlelink) {
                getDetail(articlelink, res => {
                    console.log(`res-->`, res)
                    let {activitycontentDisc} = res.data.data;

                    this.setData({
                        article:activitycontentDisc
                    })
                })

            }

            this.setData(
                {
                    content: data.data,
                }
            )
        })
        this.setData({
            options: options.id
        })

    },

    onChange(event) {
        this.setData({
            result: event.detail,
        });
    },

    onClick(event) {
        const {name} = event.currentTarget.dataset;
        console.log(name)
        this.setData({
            result: name,
        });
    },
    onSubmit() {
        const res = this.data.result
        if (!res) return wx.showToast({
            title: "您还没有选择喔",
            icon: 'error',
            duration: 2000
        })
        console.log(this.options)
        electionVote({
            electioncontentId: res,
            electionId: parseInt(this.options.id)
        }, res => {
            console.log(`res-->`, res)

            if (res.data.code !== 0) return wx.showToast({
                title: res.data.msg,
                icon: 'error',
                duration: 2000
            })
            return wx.showToast({
                title: res.data.msg,
                icon: 'success',
                duration: 2000
            })
        })


    }
})
